//
//  YWReportTroubleSelectEquipmentSystemTypeTableViewController.m
//  maintain
//
//  Created by 烟雨云渊 on 2017/4/11.
//  Copyright © 2017年 ewin. All rights reserved.
//

#import "YWReportTroubleSelectEquipmentSystemTypeTableViewController.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"

#import "YWEquipmentType.h"
#import "YWEquipmentManager.h"

@interface YWReportTroubleSelectEquipmentSystemTypeTableViewController ()
@property (nonatomic, strong) NSMutableArray *allEquipmentSystemType;
@property (nonatomic, strong) NSMutableArray *selectTypeArray;
@end

@implementation YWReportTroubleSelectEquipmentSystemTypeTableViewController

//- (instancetype)init{
//    return [super initWithStyle:UITableViewStyleGrouped];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"报障类别";
    self.tableView.editing = YES;
    self.tableView.backgroundColor = [UIColor customBackgroudColor];
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(sureButtonEvent)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)allEquipmentSystemType{
    if (!_allEquipmentSystemType) {
        _allEquipmentSystemType = [NSMutableArray array];
        [_allEquipmentSystemType addObjectsFromArray:[[YWEquipmentManager shareManager] allEquipmentSystemType]];
    }
    return _allEquipmentSystemType;
}
- (NSMutableArray*)selectTypeArray{
    if (!_selectTypeArray) {
        _selectTypeArray = [NSMutableArray array];
        [_selectTypeArray addObjectsFromArray:self.haveSelectTypeArr];
    }
    return _selectTypeArray;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [self.allEquipmentSystemType count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId  = @"equipmentSystemTypeReuseIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.textLabel.font = kContentFont;
    }
    YWEquipmentSystemType *sysTemType = self.allEquipmentSystemType[indexPath.row];
    cell.textLabel.text = sysTemType.systemTypeName?:@"";
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"请选择报障类别";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWEquipmentSystemType *sysTemType = self.allEquipmentSystemType[indexPath.row];
    [self.selectTypeArray addObject:sysTemType];
    [self.tableView reloadData];
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
    YWEquipmentSystemType *sysTemType = self.allEquipmentSystemType[indexPath.row];
    NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %d", @"systemTypeId", sysTemType.systemTypeId];
    NSArray *array = [[self.selectTypeArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
    if (array.count>0) {
        [self.selectTypeArray removeObjectsInArray:array];
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    YWEquipmentSystemType *sysTemType = self.allEquipmentSystemType[indexPath.row];
    [self.selectTypeArray enumerateObjectsUsingBlock:^(YWEquipmentSystemType *     _Nonnull equsysType, NSUInteger idx, BOOL * _Nonnull stop) {
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %d", @"systemTypeId", sysTemType.systemTypeId];
        NSArray *array = [[self.selectTypeArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count>0) {
            [tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
            *stop = YES;
        }else{
            [tableView deselectRowAtIndexPath:indexPath animated:NO];
        }
    }];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

- (void)sureButtonEvent{
    if ([self.delegate respondsToSelector:@selector(popSelectedEquipmentSystemTypeArray:)]) {
            [self.delegate popSelectedEquipmentSystemTypeArray:self.selectTypeArray];
            [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
